//
//  MHReplyListVC.m
//  ManHua
//
//  Created by 江子安 on 15/10/27.
//  Copyright © 2015年 蓝欧科技. All rights reserved.
//

#import "MHReplyListVC.h"
#import "MHReplyListCell.h"
#import <AFNetworking.h>

#define kReplyURL @"http://112.124.96.190:9090/bukamanhua5appjobs/ComicHandle.ashx"


@interface MHReplyListVC ()<UITextFieldDelegate,UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataSourceArr;

@property (nonatomic, strong) UITextField *inputTF;
@property (nonatomic, strong) UIButton *commentBtn;
@property (nonatomic, strong) UIView *inputview;

@end

@implementation MHReplyListVC

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

- (NSMutableArray *)dataSourceArr
{
    if (!_dataSourceArr) {
        _dataSourceArr = [NSMutableArray arrayWithCapacity:0];
        
    }
    return _dataSourceArr;
}



- (void)requestData
{
//    ?commentid=12413&method=replylist&pageindex=0
    NSDictionary *dict = @{@"commentid":self.commentid,@"method":@"replylist",@"pageindex":@"0"};
    [[AFHTTPRequestOperationManager manager] GET:kReplyURL parameters:dict success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        NSArray *arr = responseObject;
        for (NSDictionary *replyDict in arr) {
            MHComicComentModel *model = [[MHComicComentModel alloc]init];
            [model setValuesForKeysWithDictionary:replyDict];
            [self.dataSourceArr addObject:model];
        }
        [self.tableView reloadData];
        
        
    } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
        
    }];
}


// 添加一个输入框
- (void)addCommentInputView
{
    self.inputview = [[UIView alloc]initWithFrame:CGRectMake(0, Height - 40, Width, 40)];
    _inputview.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_inputview];
    
    _inputTF = [[UITextField alloc]initWithFrame:CGRectMake(15, 5, Width - 30 - 60 - 15, 30)];
    _inputTF.delegate = self;
    _inputTF.placeholder = @"写评论...";
    _inputTF.borderStyle = UITextBorderStyleRoundedRect;
    [_inputview addSubview:_inputTF];
    
    _commentBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _commentBtn.frame = CGRectMake(_inputTF.frame.size.width + 30, 5, 60, 30);
    _commentBtn.backgroundColor = [UIColor colorWithRed:0.246 green:0.922 blue:0.204 alpha:1.000];
    [_commentBtn setTitle:@"评论" forState:UIControlStateNormal];
    [_inputview addSubview:_commentBtn];
    
}




- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    /**
     *     这是用户评论的回复页面
     */
    [self requestData];
    
    
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 40) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    [_tableView registerClass:[MHReplyListCell class] forCellReuseIdentifier:@"cell"];
    
    UIView *view = [[UIView alloc]init];
    _tableView.tableFooterView = view;
    
    [self.view addSubview:_tableView];
    
    [self addCommentInputView];
    
    
//     给键盘添加监控
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];

}

#pragma mark - 文本框代理
/**
 *  点击了return按钮(键盘最右下角的按钮)就会调用
 */
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    NSLog(@"%@", textField.text);
    [self.view endEditing:YES];
    // 返回YES即可
    return YES;
}
/**
 *  当键盘改变了frame(位置和尺寸)的时候调用
 */
- (void)keyboardWillChangeFrame:(NSNotification *)note
{
   
    
    // 0.取出键盘动画的时间
    CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    
    // 1.取得键盘最后的frame
    CGRect keyboardFrame = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    
    // 2.计算控制器的view需要平移的距离
    CGFloat transformY = keyboardFrame.origin.y - self.view.frame.size.height;
    
    // 3.执行动画
    [UIView animateWithDuration:duration animations:^{
        self.view.transform = CGAffineTransformMakeTranslation(0, transformY);
    }];
}
/**
 *  当开始拖拽表格的时候就会调用
 */
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    // 退出键盘
    [self.view endEditing:YES];
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MHComicComentModel *model = self.dataSourceArr[indexPath.row];
   
    return  [MHReplyListCell heightOfCellWithModdel:model];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSourceArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MHReplyListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    MHComicComentModel *model = self.dataSourceArr[indexPath.row];
    
    [cell configureDataWithModel:model];
    
    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
